﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using EDTS.Model.Commons;

namespace EDTS.Data.Commons
{
    public class FileCommand
    {
        public static void CreateNewFile(FileModel file)
        {
            if (file != null)
            {
                using (DBCentre db = new DBCentre())
                {
                    var dFile = Convertor.ConvertFrom<File, FileModel>(file);
                    db.DataContext.Files.Add(dFile);
                    db.DataContext.SaveChanges();
                }
            }
        }

        public static FileModel GetFile(Guid guid,bool noData=false)
        {
            if (guid != Guid.Empty)
            {
                using (DBCentre db = new DBCentre())
                {
                    var dFile = db.DataContext.Files.FirstOrDefault(p => p.Guid == guid);
                    if (dFile != null)
                    {
                        var file = Convertor.ConvertTo<FileModel, File>(dFile);
                        if (noData)
                            file.Data = null;
                        return file;
                    }
                }
            }
            return null;
        }
    }
}
